const mysql = require("../lib/mysql");
var token = require("./token");
module.exports.login = async function(req,res,next){
	 var  body = req['body'],
	 password = body.password,
	 username = body.username;
	 var selectArray=['id', 'userName','wx_img','nickname','userimg','gender','email','profession','age','instroduction','isadmin'];
	 try{
		 var data = await  mysql.table('blogger').where({username:'root',password:password}).field(selectArray).select();
		 if(data.length==1){

		      var api_token = token.createToken(username,data[0].id,'7','hour');
		 	   req.session.api_token = api_token;
		       res.redirect('/admin');

		 }else{
		 	 res.redirect('/admin/login');
		 }

		}catch(err){
			 res.redirect('/admin/login');
		}
}